<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-21 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use Jrk\Tree;

class Invoice extends CommonBaseModel implements Comm
{

    public function getAdminPageData($param = [], $order = 'create_time desc',$where=[])
    {
        $model = self::where($where);
        if (isset($param['merchant_id']) && $param['merchant_id']){
            $where[] = ['merchant_id','=',$param['merchant_id']];
        }
//        $data = $this
//            ->alias('I')
//            ->leftjoin('tang_cost c','I.costId=c.id')
//            ->leftjoin(['tang_merchant' => 'm'], "c.merchant_id = m.id")
//            ->where($where)
//            ->order('I.id desc')
//            ->field('I.*,m.name,m.company,m.code,c.amount,c.electrity_type')->select()->toArray();
//
//        //page??总页数
//        $page = $param['page'];
//        $limit = $param['limit'];
//        $datas = array_splice($data, ($page - 1) * $limit, $limit);
//        $count = count($data);
//        return parent::ajaxResult($datas, $count);
        $data = $model->with(['merchant'])->order('id desc')->where($where)->page(PAGE)->limit(LIMIT)->select()->toArray();
        foreach ($data as $k=>$v){
            $data[$k]['amount'] = (new Cost())->whereIn('id',$v['costId'])->sum('all_amount');
        }
        $count = count($data);
        return parent::ajaxResult($data,$count);

    }

    public function delById($id)
    {
        // TODO: Implement delById() method.
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function cost(){
        return $this->hasOne(Cost::class,'id','costId')->field('id,amount,cost_type_id');
    }
    public function merchant(){
        return $this->hasOne(Merchant::class,'id','merchant_id')->bind([
            'company'=>'company',
            'name'=>'name',
            'code'=>'code'
        ]);
    }
}